{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Implementation of Depth-First Search\n",
    "This algorithm we will be discussing is Depth-First search which as the name hints at, explores possible vertices (from a supplied root) down each branch before backtracking. This property allows the algorithm to be implemented succinctly in both iterative and recursive forms. Below is a listing of the actions performed upon each visit to a node.\n",
    "\n",
    "* Mark the current vertex as being visited.\n",
    "* Explore each adjacent vertex that is not included in the visited set.\n",
    "\n",
    "We will assume a simplified version of a graph in the following form:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "graph = {'A': set(['B', 'C']),\n",
    "         'B': set(['A', 'D', 'E']),\n",
    "         'C': set(['A', 'F']),\n",
    "         'D': set(['B']),\n",
    "         'E': set(['B', 'F']),\n",
    "         'F': set(['C', 'E'])}"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Connected Component\n",
    "\n",
    "The implementation below uses the stack data-structure to build-up and return a set of vertices that are accessible within the subjects connected component. Using Python’s overloading of the subtraction operator to remove items from a set, we are able to add only the unvisited adjacent vertices."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'A', 'B', 'C', 'D', 'E', 'F'}"
      ]
     },
     "execution_count": 15,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "def dfs(graph, start):\n",
    "    visited, stack = set(), [start]\n",
    "    while stack:\n",
    "        vertex = stack.pop()\n",
    "        if vertex not in visited:\n",
    "            visited.add(vertex)\n",
    "            stack.extend(graph[vertex] - visited)\n",
    "    return visited\n",
    "\n",
    "dfs(graph, 'A') "
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "The second implementation provides the same functionality as the first, however, this time we are using the more succinct recursive form. Due to a common Python gotcha with default parameter values being created only once, we are required to create a new visited set on each user invocation. Another Python language detail is that function variables are passed by reference, resulting in the visited mutable set not having to reassigned upon each recursive call."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'A', 'B', 'C', 'D', 'E', 'F'}"
      ]
     },
     "execution_count": 12,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "def dfs(graph, start, visited=None):\n",
    "    if visited is None:\n",
    "        visited = set()\n",
    "    visited.add(start)\n",
    "    for nxt in graph[start] - visited:\n",
    "        dfs(graph, nxt, visited)\n",
    "    return visited\n",
    "\n",
    "dfs(graph, 'A') "
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Paths\n",
    "We are able to tweak both of the previous implementations to return all possible paths between a start and goal vertex. The implementation below uses the stack data-structure again to iteratively solve the problem, yielding each possible path when we locate the goal. Using a generator allows the user to only compute the desired amount of alternative paths."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[['A', 'B', 'E', 'F'], ['A', 'C', 'F']]"
      ]
     },
     "execution_count": 11,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "def dfs_paths(graph, start, goal):\n",
    "    stack = [(start, [start])]\n",
    "    while stack:\n",
    "        (vertex, path) = stack.pop()\n",
    "        for nxt in graph[vertex] - set(path):\n",
    "            if nxt == goal:\n",
    "                yield path + [nxt]\n",
    "            else:\n",
    "                stack.append((nxt, path + [nxt]))\n",
    "\n",
    "list(dfs_paths(graph, 'A', 'F'))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Resources\n",
    "* [Depth-and Breadth-First Search](http://jeremykun.com/2013/01/22/depth-and-breadth-first-search/)\n",
    "* [Connected component](https://en.wikipedia.org/wiki/Connected_component_(graph_theory))\n",
    "* [Adjacency matrix](https://en.wikipedia.org/wiki/Adjacency_matrix)\n",
    "* [Adjacency list](https://en.wikipedia.org/wiki/Adjacency_list)\n",
    "* [Python Gotcha: Default arguments and mutable data structures](https://developmentality.wordpress.com/2010/08/23/python-gotcha-default-arguments/)\n",
    "* [Generators](https://wiki.python.org/moin/Generators)"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 2",
   "language": "python",
   "name": "python2"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 2
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython2",
   "version": "2.7.11"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 0
}
